package com.neoCalf.neoFriend.controller;

import com.neoCalf.neoCommon.entity.UserContact;
import com.neoCalf.neoCommon.entity.UserContactApply;
import com.neoCalf.neoCommon.enums.ResultCode;
import com.neoCalf.neoCommon.enums.UserContactTypeEnum;
import com.neoCalf.neoCommon.exception.BusinessException;
import com.neoCalf.neoCommon.model.R;
import com.neoCalf.neoCommon.model.dto.AddGroupUserDto;
import com.neoCalf.neoCommon.model.dto.RemoveGroupUserDto;
import com.neoCalf.neoCommon.model.dto.SearchContactResultDto;
import com.neoCalf.neoCommon.model.dto.UserInfoDto;
import com.neoCalf.neoFriend.service.ContactService;
import org.jetbrains.annotations.NotNull;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotEmpty;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;


/**
 * @author 小牛
 * @version 1.0
 * @description TODO
 * @date 2025/4/13 21:12
 */
@RestController
@Validated
@RequestMapping("/contact")
public class ContactController {
    @Resource
    private ContactService contactService;


    @PostMapping("/addRobotContact")
    public R<String> addRobotContact(@RequestBody @NotNull UserContact userContact) {
        contactService.addRobotContact(userContact);
        return R.ok();
    }

    @PostMapping("addGroupContact")
    R<String> addGroupContact(@RequestBody UserContact userContact) {
        contactService.addGroupContact(userContact);
        return R.ok();
    }

    @PostMapping("selectContactList")
    public R<List<UserContact>> selectContactList(@RequestParam("userId") @NotEmpty String userId) {
        return R.ok(contactService.selectContactList(userId));
    }

    @PostMapping("/selectUserContactApplyList")
    public R<List<UserContactApply>> selectUserContactApplyList(@RequestParam("userId") @NotEmpty String userId) {
        return R.ok(contactService.selectUserContactApplyList(userId));
    }

    @PostMapping("/findUserContact")
    public R<UserContact> findUserContact(@RequestParam("userId") @NotEmpty String userId,
                                          @RequestParam("contactId") @NotEmpty String contactId) {
        return R.ok(contactService.findUserContact(userId, contactId));
    }

    @PostMapping("/findUserContactByType")
    public R<List<UserContact>> findUserContactByType(@RequestParam("userId") @NotEmpty String userId,
                                                      @RequestParam("contactType") @NotNull Integer contactType) {
        return R.ok(contactService.findUserContactByType(userId, contactType));
    }

    @PostMapping("/findMyContactCount")
    public R<Long> findMyContactCount(@RequestParam("userId") @NotEmpty String userId,
                                      @RequestParam("typeList") @NotEmpty Integer[] typeList) {
        Long count = contactService.findMyContactCount(userId, typeList);
        return R.ok(count);
    }

    @PostMapping("/findGroupMemberCount")
    R<Long> findGroupMemberCount(@RequestParam("groupId") String groupId) {
        Long count = contactService.findGroupMemberCount(groupId);
        return R.ok(count);
    }

    @PostMapping("/findGroupMember")
    R<List<UserContact>> findGroupMember(@RequestParam("groupId") String groupId) {
        List<UserContact> contactList = contactService.findGroupMember(groupId);
        return R.ok(contactList);
    }

    @GetMapping("/loadContact")
    public R<List<UserContact>> loadContact(HttpServletRequest request, @NotNull Integer contactType) {
        String userId = request.getHeader("userId");
        UserContactTypeEnum typeEnum = UserContactTypeEnum.getByType(contactType);
        if (typeEnum == null) {
            throw new BusinessException(ResultCode.CODE_10017);
        }
        List<UserContact> userContactList = contactService.loadContact(userId, typeEnum);
        return R.ok(userContactList);
    }

    @GetMapping("/addContact2BlackList")
    public R<String> addContact2BlackList(HttpServletRequest request, @NotEmpty String contactId) {
        String userId = request.getHeader("userId");
        contactService.addContact2BlackList(userId, contactId);
        return R.ok();
    }

    @GetMapping("/removeContact2BlackList")
    public R<String> removeContact2BlackList(HttpServletRequest request, @NotEmpty String contactId) {
        String userId = request.getHeader("userId");
        contactService.removeContact2BlackList(userId, contactId);
        return R.ok();
    }

    @GetMapping("/delContact")
    public R<String> delContact(HttpServletRequest request, @NotEmpty String contactId) {
        String userId = request.getHeader("userId");
        contactService.delContact(userId, contactId);
        return R.ok();
    }

    @GetMapping("/contactInfo")
    public R<UserInfoDto> contactInfo(HttpServletRequest request, @NotEmpty String contactId) {
        String userId = request.getHeader("userId");
        UserInfoDto userInfoDto = contactService.contactInfo(userId, contactId);
        return R.ok(userInfoDto);
    }

    @GetMapping("/search")
    public R<SearchContactResultDto> search(HttpServletRequest request, @NotEmpty String contactId) {
        String userId = request.getHeader("userId");
        SearchContactResultDto searchContactResultDtoList = contactService.search(userId, contactId);
        return R.ok(searchContactResultDtoList);
    }

    @PostMapping("/loadApply")
    public R<List<UserContactApply>> loadContactApply(HttpServletRequest request) {
        String userId = request.getHeader("userId");
        List<UserContactApply> userContactApplyList = contactService.loadContactApply(userId);
        return R.ok(userContactApplyList);
    }

    @GetMapping("/applyAdd")
    public R<Integer> applyAddContact(HttpServletRequest request, @NotEmpty String contactId, @NotNull String applyInfo) throws UnsupportedEncodingException {
        String userId = request.getHeader("userId");
        String nickName = URLDecoder.decode(request.getHeader("nickName"), "UTF-8");
        Integer auditType = contactService.applyAddContact(userId, nickName, contactId, applyInfo);
        return R.ok(auditType);
    }

    @GetMapping("/dealWithApply")
    public R<String> dealWithApply(HttpServletRequest request, @NotNull Integer applyId, @NotNull Integer status) throws UnsupportedEncodingException {
        String userId = request.getHeader("userId");
        String nickName = URLDecoder.decode(request.getHeader("nickName"), "UTF-8");
        contactService.dealWithApply(userId, nickName, applyId, status);
        return R.ok();
    }

    @PostMapping("/addGroupUser")
    public R<List<UserContact>> addGroupUser(HttpServletRequest request, AddGroupUserDto addGroupUserDto) {
        String userId = request.getHeader("userId");
        List<UserContact> userContactList = contactService.addGroupUser(userId, addGroupUserDto);
        return R.ok(userContactList);
    }

    @PostMapping("/removeGroupUser")
    public R<String> removeGroupUser(HttpServletRequest request, RemoveGroupUserDto groupUserDto) {
        String userId = request.getHeader("userId");
        contactService.removeGroupUser(userId, groupUserDto);
        return R.ok();
    }
}
